﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;

namespace desarrolloMobile.DGTViewer
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            
            //Checking Network connectivity
            if (!NetworkInterface.GetIsNetworkAvailable())
                MessageBox.Show("Esta aplicación requiere conexión de datos. La aplicación no puede continuar",
                                "Conexión de datos", MessageBoxButton.OK);
            else
                // Set the data context of the listbox control to the sample data
                DataContext = App.ViewModel;
            
            _performanceProgressBar.Show();
            this.Loaded += MainPage_Loaded;

            
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           //if (e.NavigationMode == NavigationMode.Back)  App.ViewModel.Actualizar();
        }

        // Handle selection changed on ListBox
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.IsDataLoaded)
            {
                DataContext = App.ViewModel;
            }

            if (App.ViewModel.PredicadoCampo.IsValid())
            {
                if ((App.ViewModel.PredicadoCampo != "Fecha") &&
                    (App.ViewModel.PredicadoCampo != "Hora"))
                {
                    this.txtFiltro.Text = string.Format("Filtrado por {0} ({1})"
                                                        , App.ViewModel.PredicadoCampo, App.ViewModel.PredicadoValor);
                }
                else if (App.ViewModel.PredicadoCampo == "Fecha")
                {
                    this.txtFiltro.Text = "Incidencias últimas 24 horas";
                }
                else if (App.ViewModel.PredicadoCampo == "Hora")
                {
                    this.txtFiltro.Text = "Incidencias última hora";
                }

            }
            else
            {
                this.txtFiltro.Text = "Mostrando todas las incidencias";
            }

            _performanceProgressBar.Hide();
        }

        private void filtrosButton_Click(object sender, EventArgs e)
        {
            if (App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.Actualizar();
                NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
            }
        }

        private void acercaButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/acercade.xaml", UriKind.Relative));
        }

        private void CambioFuente(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Filtros.xaml", UriKind.Relative));
        }
    }
}